package org.shoukaiseki.ddcs.actuator.conn;

import com.alibaba.druid.pool.DruidDataSource;
import org.shoukaiseki.ddcs.actuator.config.ConfigYaml;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * org.shoukaiseki.ddcs.actuator.ConnectionFactory <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  2018-07-22 17:27:47<br>
 * ブログ http://shoukaiseki.blog.163.com/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/

public class ConnectionFactory {

    private static Connection CONN_CONFIG=null;

    public static Connection getConnectionConfig() {
        try {
            if(CONN_CONFIG==null){

                DruidDataSource ds = new DruidDataSource();
                String driver = ConfigYaml.getString("ddcs.datasource.configdb.driver");
                String username = ConfigYaml.getString("ddcs.datasource.configdb.username");
                String password = ConfigYaml.getString("ddcs.datasource.configdb.password");
                String url = ConfigYaml.getString("ddcs.datasource.configdb.url");
                ds.setDriverClassName(driver);
                ds.setUsername(username);
                ds.setPassword(password);
                ds.setUrl(url);
                System.out.println("url="+url);
                CONN_CONFIG= ds.getConnection();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CONN_CONFIG;
    }

    public static Connection createNonSharedConn(String datasourceName) throws SQLException {
        DruidDataSource ds = new DruidDataSource();
        String driver = ConfigYaml.getString("ddcs.datasource.name." + datasourceName + ".driver");
        String username = ConfigYaml.getString("ddcs.datasource.name." + datasourceName + ".username");
        String password = ConfigYaml.getString("ddcs.datasource.name." + datasourceName + ".password");
        String url = ConfigYaml.getString("ddcs.datasource.name." + datasourceName + ".url");
        ds.setDriverClassName(driver);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setUrl(url);
        System.out.println("url="+url);
        System.out.println("datasourceName="+datasourceName);
        return ds.getConnection();


    }

}
